package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWorkflowNodeInLogic;

public class WorkflowInLogicNodeLoader extends NodeLoader {

	public List<Shape> loadNodes(Element root) {
		
		NodeList workflowNodes = root.getElementsByTagName(SaturnWorkflowNodeInLogic.KEY_NODE_WORKFLOW);
		List<Shape> shapes = new ArrayList<Shape>(workflowNodes.getLength());
		
		for (int i = 0; i < workflowNodes.getLength(); ++i){
			SaturnWorkflowNodeInLogic saturnWorkflowNodeInLogic = new SaturnWorkflowNodeInLogic();
			
			Node workflowNode = workflowNodes.item(i);
			
			String shapeId = super.getNodeAttributeValue(workflowNode, Shape.KEY_ATTRIBUTE_SHAPE_ID);
			
			String nextId = super.getNodeAttributeValue(workflowNode, Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);
			
			String exception = super.getNodeAttributeValue(workflowNode,
					SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_EXCEPTION_IN_LOGIC);
			
			String key = super.getNodeElementValue(workflowNode,
					SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_KEY_IN_LOGIC);
			
			String name = super.getNodeElementValue(workflowNode, 
					SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_NAME_IN_LOGIC);
			
			String version = super.getNodeElementValue(workflowNode, 
					SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_VERSION_IN_LOGIC);
			
			String description = super.getNodeElementValue(workflowNode,
					SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_DESCRIPTION_IN_LOGIC);
			
//			String transactionType = super.getNodeElementValue(workflowNode, 
//					SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_VERSION_IN_LOGIC);
			
			String transaction = this.getTransactionTypeValue(workflowNode);
			
			List<SaturnParameter> parameters = super.getParameters(workflowNode);
			
			Point location = super.getLocation(workflowNode);
			Dimension size = new Dimension(50, 70);
			
			saturnWorkflowNodeInLogic.setId(shapeId);
			saturnWorkflowNodeInLogic.setLocation(location);
			saturnWorkflowNodeInLogic.setKey(key);
			saturnWorkflowNodeInLogic.setName(name);
			saturnWorkflowNodeInLogic.setVersion(version);
			saturnWorkflowNodeInLogic.setTransactionType(transaction);
			saturnWorkflowNodeInLogic.setNextId(nextId);
			saturnWorkflowNodeInLogic.setException(exception);
			saturnWorkflowNodeInLogic.setDescription(description);
			saturnWorkflowNodeInLogic.setParameters(parameters);
			saturnWorkflowNodeInLogic.setSize(size);
			saturnWorkflowNodeInLogic.setNode(workflowNode);
			
			shapes.add(saturnWorkflowNodeInLogic);
		}
		
		return shapes;
	}
	
	/**
	 * 载入Workflow的transactionType
	 * 
	 */
	private String getTransactionTypeValue(Node workflowNode) {
		NodeList nodelist = workflowNode.getChildNodes();
		
		for(int i = 0; i<nodelist.getLength();i++){
			if(nodelist.item(i).getNodeName().equals(SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_TRANSACTION_IN_LOGIC)){
				return super.getNodeAttributeValue(nodelist.item(i), SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_TYPE_IN_LOGIC);
			}
		}
	
//		Node tTypeNode = super.getChildNode(node,SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_TRANSACTION_IN_LOGIC);
//		
//		String tTypeTemp = super.getNodeAttributeValue(tTypeNode,
//				SaturnWorkflowNodeInLogic.SHOW_WORKFLOW_TYPE_IN_LOGIC);
		
		return "";		
	}	
}
